<?php

namespace plugin\muadmin\app\admin\controller\general;

use plugin\muadmin\app\admin\dict\FormTypeConfigDict;
use plugin\muadmin\app\admin\dict\FormTypeList;
use plugin\muadmin\app\common\base\BaseAdminController;
use plugin\muadmin\app\common\model\SysConfig;
use plugin\muadmin\app\common\model\SysConfigGroup;
use think\facade\Db;

class System extends BaseAdminController
{
    public function initialize()
    {
        parent::initialize();
        $this->model = new SysConfig();
        $group = SysConfigGroup::where('status', 1)->select()->toArray();
        $this->assignConfig('group', $group);
        $formType = FormTypeConfigDict::getInstance()->getOptions();
//        print_r($formType);
        $this->assignConfig('form_type', $formType);
    }
    public function index()
    {
        if($this->request->isAjax()){
            $group = $this->param('group');
            $list = $this->model->whereIn('group', $group)->select()->toArray();
            if(!empty($list)){
               $newlist = [];
               foreach ($list as $k=>$v){
                   if(in_array($v['type'],['number_range'])){
                       if(empty($v['value'])){
                           $v['value'] = [
                               'start'=>'',
                               'end'=>''
                           ];
                       }else{
                           $v['value'] = json_decode($v['value'],true);
                       }
                   }
                   if(in_array($v['type'],['checkbox','selects','imgs'])){
                       if(!empty($v['value'])){
                           $v['value'] = json_decode($v['value'],true);
                       }else{
                           $v['value'] = [];
                       }
                   }
                   if(empty($newlist[$v['group']])){
                       $newlist[$v['group']] = [];
                   }
                   $newlist[$v['group']][] = $v;
               }
               $list = $newlist;
            }
            return $this->success('success', $list);
        }
        return $this->fetch();
    }

    /**
     * 添加分组
     * @return \support\Response
     */
    public function add_group()
    {
        if($this->request->isAjax()){
            $data = $this->param('row');
            $data['key'] = trim($data['key']);
            $has = SysConfigGroup::where('key',$data['key'])
                ->find();
            if(!empty($has)){
                return $this->error('分组已存在');
            }
            $res =SysConfigGroup::create($data);
            if(!$res){
                return $this->error('添加失败');
            }
            return $this->success('ok',$data);
        }
        return $this->fetch();
    }
    public function add()
    {
        $group = $this->param('group');
        if($this->request->isAjax()){
            $data = $this->param('row');
            $res = $this->model->create($data);
            if(!$res){
                return $this->error('添加失败');
            }
            return $this->success('添加成功');
        }
        $this->assignConfig('group',$group);
        return $this->fetch();
    }

    /**
     * 编辑
     * @return \support\Response
     */
    public function edit()
    {
        $data = $this->param('row');
        foreach ($data as $k=>$v){
            if(!empty($v['value'])){
                if(is_array($v['value'])){
                    $v['value'] = json_encode($v['value']);
                }
            }
            $this->model->where('id',$v['id'])->update($v);
        }
        return $this->success('ok', $data);
    }
    /**
     * 获取分组列表
     * @return \support\Response
     */
    public function get_group_list()
    {
        $groups = SysConfigGroup::where('status', 1)->select()->toArray();
        return $this->success('ok', $groups);
    }
    public function del_group()
    {
        $key = $this->param('key');
        if(empty($key)){
            return $this->error('请选择分组');
        }
        // 启动事务
        Db::startTrans();
        try {
           SysConfigGroup::destroy(['key'=>$key]);
           SysConfig::destroy(['group'=>$key]);
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return $this->error('删除失败');
        }
        return $this->success('删除成功');
    }
}